﻿using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Zenject;

public class GamePanelUIManager : MonoBehaviourSingleton<GamePanelUIManager>
{
    [SerializeField] private TextMeshProUGUI hpText;
    [SerializeField] private Slider hpSlider;
    [SerializeField] private TextMeshProUGUI expText;
    [SerializeField] private Slider expSlider;
    [SerializeField] private TextMeshProUGUI levelText;
    [SerializeField] private TextMeshProUGUI coinText;

    [SerializeField] private TextMeshProUGUI waveText;
    [SerializeField] private TextMeshProUGUI waveTimeText;

    [SerializeField] private GameObject gameOverPanelUI;
    [SerializeField] private GameObject gameWinPanelUI;
    [SerializeField] private GameObject waveWinPanelUI;
    [Inject] private GameManager _gameManager;


    private void OnEnable()
    {
        RxEventBus.Subscribe<PlayerHpChangeEvent>().Subscribe(e =>
        {
            {
                UpdateHpText(e);
            }
        }).AddTo(this);
        RxEventBus.Subscribe<PlayerExpChangeEvent>().Subscribe(e =>
        {
            {
                UpdateExpText(e);
            }
        }).AddTo(this);
        RxEventBus.Subscribe<PlayerCoinChangeEvent>().Subscribe(e =>
        {
            {
                UpdateCoinText(e);
            }
        }).AddTo(this);
        RxEventBus.Subscribe<WaveTimeChangeEvent>().Subscribe(e =>
        {
            {
                UpdateWaveText(e);
            }
        }).AddTo(this);
        RxEventBus.Subscribe<GameOverEvent>().Subscribe(e =>
        {
            {
                GameOver(e);
            }
        }).AddTo(this);
        RxEventBus.Subscribe<GameWinEvent>().Subscribe(e =>
        {
            {
                GameWin(e);
            }
        }).AddTo(this);
        RxEventBus.Subscribe<WaveWinEvent>().Subscribe(e =>
        {
            {
                WaveWin(e);
            }
        }).AddTo(this);
    }

    private void OnDisable()
    {
        RxEventBus.Unsubscribe<PlayerExpChangeEvent>();
        RxEventBus.Unsubscribe<PlayerCoinChangeEvent>();
        RxEventBus.Unsubscribe<PlayerHpChangeEvent>();
        RxEventBus.Unsubscribe<WaveTimeChangeEvent>();
        RxEventBus.Unsubscribe<GameWinEvent>();
        RxEventBus.Unsubscribe<GameOverEvent>();
        RxEventBus.Unsubscribe<WaveWinEvent>();
    }

    private void UpdateExpText(PlayerExpChangeEvent eventData)
    {
        expText.text = $"{eventData.exp.ToString()}/{eventData.expLevelUpNeed.ToString()}";
        expSlider.value = (float)eventData.exp / eventData.expLevelUpNeed;
        levelText.text = $"Lv.{eventData.level}";
    }

    private void UpdateCoinText(PlayerCoinChangeEvent eventData)
    {
        coinText.text = $"{eventData.coin.ToString()} $";
    }

    private void UpdateHpText(PlayerHpChangeEvent eventData)
    {
        hpText.text = $"{eventData.hp.ToString()}/{eventData.hpMax.ToString()}";
        hpSlider.value = (float)eventData.hp / eventData.hpMax;
    }

    private void UpdateWaveText(WaveTimeChangeEvent eventData)
    {
        waveText.text = $"当前波次：{eventData.wave.ToString()}";
        waveTimeText.text = $"剩余时间：{eventData.waveTime.ToString()} s";
    }

    private void GameOver(GameOverEvent eventData)
    {
        Pause();
        SwitchUIManager.Instance.SwitchUI(gameOverPanelUI);
    }

    private void GameWin(GameWinEvent eventData)
    {
        Pause();
        SwitchUIManager.Instance.SwitchUI(gameWinPanelUI);
    }

    public void GotoGameMenu()
    {
        SceneManager.LoadScene("01_MainMenu");
        Resume();
    }

    private void WaveWin(WaveWinEvent eventData)
    {
        Pause();
        SwitchUIManager.Instance.SwitchUI(waveWinPanelUI);
    }

    public void GotoShop()
    {
        SceneManager.LoadScene("04_Shop");
        Resume();
    }

    private void Pause()
    {
        Time.timeScale = 0;
        _gameManager.currentPlayer.GetComponent<PlayerMovement>().enabled = false;
    }
    
    private void Resume()
    {
        Time.timeScale = 1;
    }
}